/*
* vuex 状态管理工具类
* */
let utilVuexThat = null
const getMap = {
  dataviewMap: {
    remark: '数据视图配置',
    url: '/program/data-view/listObjs',
    action: 'setDataviewMap'
  },

  dictData: {
    remark: '数据字典数据',
    url: '/upms/dict_datas/list',
    action: 'setDictDataList'
  },

  dictTypeList: {
    remark: '数据字典类型',
    url: '/upms/dict_types/list',
    action: 'setDictTypeList'
  },

  hospitalList: {
    remark: '医院列表',
    url: '/base/hospitals/listUser',
    action: 'setHospitalList'
  },

  navMenuList: {
    remark: '用户菜单列表',
    url: '/upms/menus/navMenuList',
    action: 'setNavMenuList'
  },

  spList: {
    remark: '供应商列表',
    url: '/base/suppliers/listCommon',
    action: 'setSpList'
  }
}


//需要更新数据视图的getkey列表
const keysUpdateDataview = ['dictData', 'dictTypeList']
const keyLast = 'printTemplate'


const utilVuex = {
  getMapSize() {
    return Object.keys(getMap).length
  },
  //初始化各种参数
  syncAll(that, call, filterTypeArr = null) {
    let  getList = []
    Object.keys(getMap).filter(key => filterTypeArr == null || filterTypeArr.includes(key)).forEach(key => {
      getList.push(new Promise((resolve) => {
        global.$ajax.get(getMap[key].url).then(resp => {
          if (resp.success) {
            if (key == 'dictData') { //数据字典数据特殊处理
              resp.data = resp.data.map(d => {
                return { ...d, val: d.value}
              })
            }
            utilVuexThat.$store.dispatch(getMap[key].action, resp.data)
            if (filterTypeArr == null) {
              utilVuexThat.getAdd++
            }

            if (filterTypeArr != null && keysUpdateDataview.includes(key)) {
              //更新当前激活的菜单的数据字典
              let routerView = global.vue.$children[0].$children[0].$refs.routerView
              if (routerView.$children && routerView.$children.length > 0) {
                routerView.$children.forEach(dv => {
                  if (dv && dv.updateReference) {
                    dv.updateReference()
                  }
                })
              }
            }
            resolve()
          }
        })
      }))
    })

    if (filterTypeArr == null) {
      utilVuexThat = that
    }

    Promise.all(getList).then(() => {
      if (call) {
        setTimeout(() => {
          call()
        }, 500)
      }
    }).catch(e => {
      global.utilMessage.error('系统初始化失败，请刷新重试')
    })
  },

  //打印配置列表
  getPrintTemplateList() {
    return  global.vue.$store.getters.printTemplateList
  },

  //print_bus_type
  getDictDataList(keyType) {
    return  global.vue.$store.getters.dictDataMap[keyType]
  },

  //获取中心仓库
  getWarehouseCenter() {
    if (!global.vue.$store.getters.warehouseAll) {
      return []
    }
    return  global.vue.$store.getters.warehouseAll.filter(row => row.flagCenter)
  },

  //所有仓库
  getWarehouseAll() {
    if (!global.vue.$store.getters.warehouseAll) {
      return []
    }
    return  global.vue.$store.getters.warehouseAll
  },

  //获取所选择的工位
  getCurrentWarehouse() {
    return global.vue.$store.getters.currentWarehouse
  },

  //获取用户可用科室列表
  getDeptListByUser() {
    return global.vue.$store.getters.deptListByUser
  },

  //获取所有业务科室
  getDeptListBus() {
    return global.vue.$store.getters.deptListBus
  },

  //获取登录用户信息
  getCurrentUser() {
    return global.vue.$store.getters.currentUser
  },

  //获取登录用户信息
  getWarehouseByUserId() {
    return global.vue.$store.getters.warehouseListByUser
  },

  //获取登录用户信息
  isBusDept(deptId) {
    let deptList =  this.getDeptListBus()
    if (!deptList  || deptList.lengtn < 1) {
      return false
    }
    return deptList.filter(row => row.id == deptId && row.flagBus).length > 0
  },

  //视图配置
  getDataviewMap() {
    return global.vue.$store.getters.dataviewMap
  },

  //流程配置
  getFlowMap() {
    return global.vue.$store.getters.flowMap
  },

  //医院列表
  getHospitalList() {
    let list =  global.vue.$store.getters.hospitalList
    if (list && list.length > 0) {
      list.forEach(row => {
        row.value = row.id
        row.label = row.name
      })
    }
    return list
  },

  //医院列表-订单新增使用
  getHospitalListForOrderAdd() {
    let list =  global.vue.$store.getters.hospitalList
    if (list && list.length > 0) {
      list.forEach(row => {
        row.value = row.id
        row.label = row.name
      })
    }
    return list.filter(item => ![1, true].includes(item.flagDisabledOrderAdd))
  },

  //供应商列表
  getSpList() {
    let list =  global.vue.$store.getters.spList
    if (list && list.length > 0) {
      list.forEach(row => {
        row.value = row.id
        row.label = row.name
      })
    }
    return list
  },

  //医院
  getHospitalById(id) {
    let findIndex =  this.getHospitalList().findIndex(item => item.id == id )
   if (findIndex != -1) {
     return this.getHospitalList()[findIndex]
   }
    return {}
  },

  //用户菜单列表
  getNavMenuList() {
    return global.vue.$store.getters.navMenuList
  }
}

export default utilVuex
